#!/bin/sh

# Run an experiment using run-twitter, sending its output to a unique file
# and using 'nohup' so that exiting the session won't kill a background script.
# Allow -i STR or --id STR to be specified at the beginning of the options,
# specifying an ID to use in the outfiles.

if [ -z "$TEXTGROUNDER_PYTHON" ]; then
  TEXTGROUNDER_PYTHON="$HOME/devel/textgrounder/python"
fi

if [ "$1" = "-i" -o "$1" = "--id" ]; then
  ID=".$2"
  shift 2
fi

if [ "`uname`" = "Darwin" ]; then
  # Darwin (BSD) is missing all sorts of stuff, naturally, including %P,
  # which gets you lowercase am or pm. (%p gets you uppercase AM or PM --
  # real logical, huh?)
  OUTFILE="run-twitter.out$ID.`date '+%F.%H%M%p'`"
else
  OUTFILE="run-twitter.out$ID.`date '+%F.%H%M%P'`"
fi
if [ -e "$OUTFILE" ]; then
  echo "Output file $OUTFILE already exists!  Won't overwrite."
  exit 1
fi

echo "Running on `hostname` at time `date`"
echo "Outputting to $OUTFILE"
if [ -n "$1" ]; then
  echo "Arguments are" ${1+"$@"}
else
  echo "Run without arguments"
fi

nohup $TEXTGROUNDER_PYTHON/run-twitter ${1+"$@"} > $OUTFILE 2>&1
